<?php

// @formatter:off
/**
 * @file query.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: query.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/query.php $
 */
// @formatter:on

class ActionControllerContentQuery extends WCActionController {
	//
	// Protected properties.
	protected $_hasQuery = false;
	/**
	 * @var WCQuery
	 */
	protected $_query = false;
	protected $_queryString = false;
	//
	// Protected methods.
	protected function loadQueryString() {
		if(isset($_REQUEST["query"])) {
			$this->_queryString = urldecode($_REQUEST["query"]);
			$this->_hasQuery = true;

			$this->_query = new WCQuery();
			$this->_query->analyzeQuery($this->_queryString);
		}
	}
	protected function subRun() {
		$ok = true;

		$this->loadQueryString();
		if($this->_hasQuery) {
			global $wcGroup;

			$items = $this->_query->getItems($wcGroup);


			$limiter = "tag";
			$limits = WCFVPager::GetLimitsFor($limiter);
			//
			// Loading comics list.
			// @{
			$showComics = false;
			$comics = array();
			$comicCount = 1;
			uasort($items["comics"], "WCComic::NameSort");
			foreach($items["comics"] as $comic) {
				if($comicCount >= $limits["min"] && $comicCount <= $limits["max"]) {
					$showComics = true;
					$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				$comicCount++;
			}
			$this->_manager->comics = $comics;
			if($showComics) {
				$this->_manager->show_comics = true;
			}
			// @}
			//
			// Loading images list.
			// @{
			$showImages = false;
			$images = array();
			$imagesCount = 1;
			uasort($items["images"], "WCImage::NameSort");
			foreach($items["images"] as $image) {
				if($imagesCount >= $limits["min"] && $imagesCount <= $limits["max"]) {
					$showImages = true;
					$images[] = WCFVBoxes::GetImagePreviewAssigns($image, false);
				}
				$imagesCount++;
			}
			$this->_manager->images = $images;
			if($showImages) {
				$this->_manager->show_images = true;
			}
			// @}

			$this->_manager->query = (string) $this->_query;
			$this->_manager->show_stats = true;
			$this->_manager->stats_comics = count($items["comics"]);
			$this->_manager->stats_images = count($items["images"]);

			$itemsCount = max($this->_manager->stats_comics, $this->_manager->stats_images);
			WCFVPager::GetAssignsIntoManagerFor($itemsCount, $limiter);
		}

		return $ok;
	}
}
